import { DialogForm } from 'custom/index'
import { addEmployeeManagement, getEmployeeManagementDetail, updateEmployeeManagement, setPassword, validFlowerName, validNo, getRoleList, dispatchRole, searchDispatchRole } from '../api'
import { sexOptions, onDutyStatusOptions, becomeRegularStatusOptions } from '../constant'
import dayjs from 'dayjs'
import DeptNoItem from '../components/DeptNoItem.vue'

const flowerNameRule = (_: any, value: string, callback: any, row: any) => {
  if (!value) {
    return callback(new Error('花名是必须的'))
  } else if (!value.startsWith('悦')) {
    return callback(new Error('花名不符合规则，必须悦字开头'))
  } else if (value.length > 3) {
    return callback(new Error('花名不符合规则，最多3个汉字'))
  } else {
    validFlowerName({ flowerName: value, userId: row.userId })
      .then(() => {
        callback()
      })
      .catch(e => {
        callback(e)
      })
  }
}

const flowerNoRule = (_: any, value: string, callback: any, row: any) => {
  if (!value) {
    return callback(new Error('工号是必须的'))
  } else {
    validNo({ no: value, userId: row.userId })
      .then(() => {
        callback()
      })
      .catch(e => {
        callback(e)
      })
  }
}



const addDialog = new DialogForm('DialogForm')

addDialog
.title('新增员工信息')
  .postApi(addEmployeeManagement)
  .useGroup('')
    .useDatePicker('entryDate').label('入职日期').setDefault(dayjs().format('YYYY-MM-DD')).required().end()
    .useItem('name').label('姓名').required().end()
    .useItem('flowerName').label('花名').validator(flowerNameRule, { required: true, trigger: 'change'}).end()
    .useSelect('sex', sexOptions).label('性别').required().end()
    .useItem('mobile').label('手机号码').required().end() 
    .useItem('no').label('工号').validator(flowerNoRule, { required: true, trigger: 'change'}).end()
    .useDictSelect('companyNo', 'company').label('公司').setProp('widthLabel', 'companyName').setProp('onChange', () => {
      addDialog.setData('deptNo', '')
      addDialog.setData('deptName', '')
    }).required().end()
    // .useDictSelect('deptNo', 'department').label('部门').setProp('widthLabel', 'deptName').required().end()
    .useCustomItem(DeptNoItem, 'deptNo').label('部门').required().end()
    .useDictSelect('postNo', 'post').label('岗位').setProp('widthLabel', 'pestName').required().end()
    .useSelect('onDutyStatus', onDutyStatusOptions).label('在职状态').required().end()
    .useSelect('becomeRegularStatus', becomeRegularStatusOptions).label('转正状态').required().end()
    .useItem('password').label(' 登录系统密码').setProp('disabled', false).end()

const editDialog = new DialogForm('DialogForm')

editDialog
  .title('修改员工信息')
  .getApi(getEmployeeManagementDetail)
  .postApi(updateEmployeeManagement)
  .useGroup('')
    .useDatePicker('entryDate').label('入职日期').setDefault(dayjs().format('YYYY-MM-DD')).required().end()
    .useItem('name').label('姓名').required().end()
    .useItem('flowerName').label('花名').validator(flowerNameRule).end()
    .useSelect('sex', sexOptions).label('性别').required().end()
    .useItem('mobile').label('手机号码').required().end() 
    .useItem('no').label('工号').validator(flowerNoRule, { required: true, trigger: 'change'}).end()
    .useDictSelect('companyNo', 'company').label('公司').setProp('widthLabel', 'companyName').setProp('onChange', () => {
      editDialog.setData('deptNo', '')
      editDialog.setData('deptName', '')
    }).required().end()
    // .useDictSelect('deptNo', 'department').label('部门').setProp('widthLabel', 'deptName').required().end()
    .useCustomItem(DeptNoItem, 'deptNo').label('部门').required().end()
    .useDictSelect('postNo', 'post').label('岗位').setProp('widthLabel', 'pestName').required().end()
    .useSelect('onDutyStatus', onDutyStatusOptions).label('在职状态').required().end()
    .useSelect('becomeRegularStatus', becomeRegularStatusOptions).label('转正状态').required().end()
    // .useItem('password').label(' 登录系统密码').setProp('disabled', true).end()

const setPassDialog = new DialogForm('DialogForm')

setPassDialog
  .title('设置密码')
  .getApi(async (_, row) => ({ userId: row.userId }))
  .postApi(setPassword)
  .useGroup()
    .useItem('password').label('密码').required()

const authorizationDialog = new DialogForm('DialogForm')

authorizationDialog
  .title('授权')
  .getApi((_, row) => searchDispatchRole({ userId: row.userId }))
  .postApi(dispatchRole)
  .useGroup('')
    .useApiSelect('roleIdList', getRoleList).setProp('multiple', true).setProp('valueKey', 'roleId').setProp('labelKey', 'roleName').end()

export { addDialog, editDialog, setPassDialog, authorizationDialog }
